import { Component, OnInit, Input,Output, EventEmitter } from '@angular/core';
import { Stop } from './stop';

import { ActivatedRoute, Params } from '@angular/router';
import { StopService } from './stop.service';

@Component({
    selector: 'stop-detail',
    templateUrl: './stop-detail.component.html',
    // styleUrls: ['./stop-detail.component.css']
})

export class StopDetailComponent implements OnInit {
    @Input()
    stop: Stop;

    @Output() onVoted = new EventEmitter<boolean>();

    trip_id: number;

    constructor(
        private stopService: StopService,
        private route: ActivatedRoute) {
    }

    ngOnInit() {
        this.route.params.forEach((params: Params) => {
            let id = +params['id'];
            this.trip_id = id;
            //this.stopService.getStop(id).then(stop => this.stop = stop);
        });
    }

    goBack() {
        window.history.back();
    }

    save(): void {
        this.stopService.save(this.stop).then(() => this.cancel());
    }

    delete(): void {
        this.stopService.delete(this.stop).then(() => this.cancel());
    }

    cancel(): void {
        this.stop = null;
        this.onVoted.emit();
    }

}